package de.fhaugsburg.games.boardgames.scrabble.gui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JLayeredPane;

import de.fhaugsburg.games.boardgames.scrabble.gui.GLetterPiece.GLetterState;
import de.fhaugsburg.games.boardgames.scrabble.scrabbleboard.LetterPiece;
import de.fhaugsburg.games.boardgames.scrabble.scrabbleboard.ScrabbleBoard;
import de.fhaugsburg.games.boardgames.scrabble.scrabbleboard.SquareMultiplier;

public class BoardView extends JLayeredPane {

	//FIXME: Bad!
	private static final String IMG = ScrabbleMainWindow.IMG;
	private static final int HEIGHT = ScrabbleBoard.BOARD_HEIGHT;
	private static final int WIDTH = ScrabbleBoard.BOARD_WIDTH;
	
	
	private ScrabbleBoard board;
	private GSquare[][] square;
//	private JPanel[][] boardLayout;
	private BufferedImage boardBorder, squareImage_simple, squareImage_doubleWord, squareImage_tripleWord, 
					squareImage_doubleLetter, squareImage_tripleLetter, squareImage_star; 
	private int dragX, dragY;
	private Component dragComponent;
	
	
	
	/**
	 * Constructor
	 * @param board
	 */
	public BoardView(ScrabbleBoard board){
		this.board = board;
		this.square = new GSquare[HEIGHT][WIDTH];
//		this.boardLayout = new JPanel[HEIGHT][WIDTH];
		
		setLayout(new GridLayout(HEIGHT, WIDTH));
//		setBackground(Color.LIGHT_GRAY);
		
		//FIXME: doesn't work?
		//setMinimumSize(new Dimension(500,500));
		setPreferredSize(new Dimension(450,450));
		
		loadGraphics();
		createBoardView();
//		this.addMouseListener(this); 
//	    this.addMouseMotionListener(this);

	}

	
	
	
	private void loadGraphics(){
		try {
			boardBorder = ImageIO.read(new File(IMG + "scrabble_board_full.png"));
			squareImage_simple = ImageIO.read(new File(IMG + "square_simple.png"));
			squareImage_doubleLetter = ImageIO.read(new File(IMG + "square_letter_double.png"));
			squareImage_tripleLetter = ImageIO.read(new File(IMG + "square_letter_triple.png"));
			squareImage_doubleWord = ImageIO.read(new File(IMG + "square_word_double.png"));
			squareImage_tripleWord = ImageIO.read(new File(IMG + "square_word_triple.png"));
			squareImage_star = ImageIO.read(new File(IMG + "square_star.png"));
			
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
	
	
	
	
	private void createBoardView(){
		for (int i = 0; i < HEIGHT; i++) {
			for (int j = 0; j < WIDTH; j++) {
				//square[i][j] = new JLabel(board.getDetails(i, j).getSymbol());
				BufferedImage squareImage = null;
				SquareMultiplier squareDetails = board.getDetails(i, j).getDetails(); 
				switch (squareDetails){
				case LETTER_DOUBLE:
					squareImage = squareImage_doubleLetter;
					break;
				case LETTER_TRIPLE:
					squareImage = squareImage_tripleLetter;
					break;
				case WORD_DOUBLE:
					squareImage = squareImage_doubleWord;
					break;
				case WORD_TRIPLE:
					squareImage = squareImage_tripleWord;
					break;
				case SIMPLE:
					squareImage = squareImage_simple;
					break;
				}
				if (i == HEIGHT/2 && j == WIDTH/2){
					squareImage = squareImage_star;
				}
//				square[i][j] = new GSquareDetails(squareImage, squareDetails.getDescription());
				square[i][j] = new GSquare(i, j);
				square[i][j].setBackgroundImage(squareImage);
				square[i][j].setToolTip(squareDetails.getDescription());
				add(square[i][j]);
			}
		}
	}
	
	
	
	
	public void updateBoardView(){
//		removeAll();
		for (int i = 0; i < HEIGHT; i++) {
			for (int j = 0; j < WIDTH; j++) {
				if (board.getPiece(i, j) != null){
					if (square[i][j].getComponentCount() == 0){
						square[i][j].removeAll();
						LetterPiece piece = board.getPiece(i, j);
						GLetterState state;
						if(piece.equals(board.getDetails(i, j).getPieceFixed())){
							 state = GLetterState.FIXED;
						}else{
							 state = GLetterState.DROPPING;
						}
						square[i][j].add(new GLetterPiece(board.getPiece(i, j), state), JLayeredPane.PALETTE_LAYER);
						square[i][j].repaint();
						//setLayout(null);		//position: absolute
						//GLetterPiece newLetter = new GLetterPiece(board.getPiece(i, j));
						//newLetter.setSize(30, 30);
						//newLetter.setLocation(square[i][j].getLocation());
						//add(newLetter, JLayeredPane.PALETTE_LAYER);
						
					}
//					BufferedImage newSquareImage = GLetterPiece.drawLetter(board.getPiece(i, j));
//					square[i][j].setBackgroundImage(newSquareImage);
//					square[i][j].repaint();
				}else{
					//square[i][j] = new GSquareDetails(squareImage_tripleWord, board.getDetails(i, j).getDetails().getDescription());
				}
			}
		}
	}
	
	
	
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
//		updateBoardView();
//		g.drawImage(boardBorder, 0, 0, getWidth(), getHeight(), this);
	}


	
}
